package c.c.b.a.a;

import c.b.a.a.a;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;

/* compiled from: RealBufferedSink */
public final class q implements f {
    public final e a = new e();
    public final v b;
    public boolean c;

    public q(v vVar) {
        Objects.requireNonNull(vVar, "sink == null");
        this.b = vVar;
    }

    public x a() {
        return this.b.a();
    }

    public f b(String str) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.s(str);
        return u();
    }

    public e c() {
        return this.a;
    }

    public void close() throws IOException {
        if (!this.c) {
            Throwable th = null;
            try {
                e eVar = this.a;
                long j = eVar.b;
                if (j > 0) {
                    this.b.g0(eVar, j);
                }
            } catch (Throwable th2) {
                th = th2;
            }
            try {
                this.b.close();
            } catch (Throwable th3) {
                if (th == null) {
                    th = th3;
                }
            }
            this.c = true;
            if (th != null) {
                Charset charset = y.a;
                throw th;
            }
        }
    }

    public f e(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.v0(i);
        u();
        return this;
    }

    public void flush() throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        e eVar = this.a;
        long j = eVar.b;
        if (j > 0) {
            this.b.g0(eVar, j);
        }
        this.b.flush();
    }

    public f g(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.u0(i);
        u();
        return this;
    }

    public void g0(e eVar, long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.g0(eVar, j);
        u();
    }

    public f h(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.h0(i);
        u();
        return this;
    }

    public boolean isOpen() {
        return this.c ^ 1;
    }

    public f j(long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.j(j);
        return u();
    }

    public f k(byte[] bArr, int i, int i2) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.t0(bArr, i, i2);
        u();
        return this;
    }

    public String toString() {
        StringBuilder g = a.g("buffer(");
        g.append(this.b);
        g.append(")");
        return g.toString();
    }

    public f u() throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        e eVar = this.a;
        long j = eVar.b;
        if (j == 0) {
            j = 0;
        } else {
            s sVar = eVar.a.g;
            int i = sVar.c;
            if (i < 8192 && sVar.e) {
                j -= (long) (i - sVar.b);
            }
        }
        if (j > 0) {
            this.b.g0(eVar, j);
        }
        return this;
    }

    public int write(ByteBuffer byteBuffer) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        int write = this.a.write(byteBuffer);
        u();
        return write;
    }

    public f y(byte[] bArr) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.s0(bArr);
        u();
        return this;
    }
}
